package com.clouddot.baipai.presenter;


import com.clouddot.baipai.contract.IScanFaceFragmentContract;
import com.clouddot.baipai.http.entity.response.loginResponse;
import com.clouddot.baipai.http.entity.response.postPhotoResponse;
import com.clouddot.baipai.infrastructure.environment.Env;
import com.clouddot.baipai.util.ThreadPool.ThreadPoolFactory;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;

import org.wxy.sdk.base.impl.BasePresenterImpl;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class ScanFaceFragmentPresenter extends BasePresenterImpl<IScanFaceFragmentContract.IView, IScanFaceFragmentContract.IModel> implements IScanFaceFragmentContract.IPresenter {
    public ScanFaceFragmentPresenter(IScanFaceFragmentContract.IView view, IScanFaceFragmentContract.IModel model) {
        super(view, model);
    }

    @Override
    public void scanFace(final String data, final String imageType, final String topnum, final String store) {
        ThreadPoolFactory.getThread().execute(new Runnable() {
            @Override
            public void run() {
                okhttp3.Call call =  mModel.scanFace(data, imageType, topnum, store);
                call.enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        mView.fail(1, e.getMessage());
                        Env.error(e);
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        String result = response.body().string();
                        if (!result.isEmpty()) {
                            postPhotoResponse response1 = new postPhotoResponse();
                            JsonObject jsonObject = new JsonParser().parse(result).getAsJsonObject();
                            JsonElement errCode = jsonObject.get("errorCode");
                            if (errCode == null) {
                                response1.errcode = jsonObject.get("errcode").getAsString();
                                response1.errmsg = jsonObject.get("errmsg").getAsString();
                                if (response1.errmsg.equals("ok")) {
                                    JsonElement element = jsonObject.getAsJsonObject("content");
                                    JsonObject content = new JsonParser().parse(element.toString()).getAsJsonObject();
                                    JsonArray array = content.getAsJsonArray("result");
                                    JsonObject result1 = new JsonParser().parse(array.get(0).toString()).getAsJsonObject();
                                    JsonArray array1 = result1.getAsJsonArray("person");
                                    JsonObject person = new JsonParser().parse(array1.get(0).toString()).getAsJsonObject();
                                    response1.idno = person.get("idno").getAsString();
                                    if (response1.idno.equals("")) {
                                        mView.fail(1, "识别通过，平台未记录该身份信息");
                                    } else {
                                        response1.score = person.get("score").getAsString();

                                        if ((int) Math.floor(Double.parseDouble(response1.score)) > Env.getAppContext().scanVerify) {
                                            response1.facepath = person.get("facepath").getAsString();
                                            response1.identity = person.get("identity").getAsString();
                                            response1.personid = person.get("personid").getAsString();
                                            mView.success(1, response1);
                                        } else {
                                            mView.fail(1, "识别度太低");
                                        }

                                    }
                                } else {
                                    mView.fail(1, "识别不通过，未找到对应人脸信息");
                                }
                            } else {
                                mView.fail(1, "识别失败，请联系管理员");
                            }
                        }
                    }
                });
            }
        });


    }

    @Override
    public void login(final String code) {
        Subscription subscription = mModel.login("CardLogin", code)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<loginResponse>() {
                               @Override
                               public void call(loginResponse response) {
                                   if (!response.res.equals("fail")) {
                                       Env.getAppContext().cardID = code;
                                       mView.success(2, response);
                                   } else {
                                       mView.fail(2, "登录失败");
                                   }
                               }
                           }
                        , new Action1<Throwable>() {
                            @Override
                            public void call(Throwable throwable) {
                                mView.fail(2, throwable.getMessage());

                            }
                        });
        addSubscription(subscription);
    }


}
